package net.zhengxinyun.performance.data;

import com.alibaba.fastjson.JSONArray;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.Util.WorkbookUtil;
import net.zhengxinyun.performance.entity.EvaluateUserEntity;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hyp1202 on 2018/12/18.
 */
@Component
public class EvaluateUserData {

    private static final String COL_NAME = "姓名";
    private static final String COL_PHONE = "电话";
    private static final String COL_DEPARTMENT = "部门";
    private static final String COL_AGE = "年龄";
    private static final String COL_GENDER = "性别";

    private static final String[] LABLE_ZERO = {"姓名","电话","部门","年龄","性别"};

    public List<EvaluateUserEntity> importEvaluateUser(String filePath) throws Exception{
        List<EvaluateUserEntity> result = new ArrayList<>();
        List<String> errors = new ArrayList<>();
        Workbook book = WorkbookUtil.getWorkbook(filePath);
        Sheet sheet = book.getSheetAt(0);
        Iterator<Cell> iterable = sheet.getRow(0).cellIterator();
        int length = 0;
        List<String> lableListZero = new ArrayList<>();
        while (iterable.hasNext()){
            String str = iterable.next().getStringCellValue();
            if (!"".equals(str)){
                lableListZero.add(str);
                if (length > 5){
                    throw new Exception("请选择正确模板上传");
                }
                length++;
            }
        }
        if (!Util.arrayEquals(lableListZero.toArray(),LABLE_ZERO)){
            throw new Exception("请选择正确模板上传");
        }
        for (int rowIdx=1;rowIdx <= sheet.getLastRowNum();rowIdx++){
            Row row = sheet.getRow(rowIdx);
            if (WorkbookUtil.isRowEmpty(row)){
                continue;
            }
            try {
                EvaluateUserEntity evaluateUserEntity = new EvaluateUserEntity();
                String name = WorkbookUtil.getStringCellValue(row,COL_NAME);
                if (StringUtils.isBlank(name)){
                    throw new Exception("姓名不能为空");
                }else {
                    evaluateUserEntity.setName(name);
                }
                String phone = WorkbookUtil.getStringCellValue(row,COL_PHONE);
                if (StringUtils.isBlank(phone)){
                    throw new Exception("电话号码不能为空");
                }else {
                    evaluateUserEntity.setPhone(phone);
                }
                String department = WorkbookUtil.getStringCellValue(row,COL_DEPARTMENT);
                if (StringUtils.isBlank(department)){
                    throw new Exception("部门不能为空");
                }else {
                    evaluateUserEntity.setDepartment(department);
                }
                String age = WorkbookUtil.getStringCellValue(row,COL_AGE);
                if (StringUtils.isNotBlank(age)) {
                    evaluateUserEntity.setAge(age);
                }
                String gender = WorkbookUtil.getStringCellValue(row,COL_GENDER);
                if (StringUtils.isNotBlank(gender)){
                    evaluateUserEntity.setGender(gender);
                }
                result.add(evaluateUserEntity);
            }catch (Exception e){
                errors.add("第" + rowIdx + "行：" + e.getMessage());
            }
        }
        if (errors.isEmpty())
            throw new Exception(JSONArray.toJSONString(errors));
        return result;
    }
}
